package com.ruoyi.web.controller.kujiale;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.core.domain.JYXResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.swagger.SwaggerExampleConstants;
import com.ruoyi.system.dto.kujiale.AlbumDataResponse;
import com.ruoyi.system.dto.kujiale.HouseTypeResponse;
import com.ruoyi.system.dto.kujiale.PageResponse;
import com.ruoyi.system.dto.kujiale.RenderingResponse;
import com.ruoyi.system.dto.kujiale.drawing.DrawingDetailData;
import com.ruoyi.system.dto.kujiale.info.ArchiveResultItem;
import com.ruoyi.system.dto.kujiale.info.LayoutLegendItem;
import com.ruoyi.system.dto.kujiale.info.RenderingImage;
import com.ruoyi.system.dto.kujiale.info.UserDesignItem;
import com.ruoyi.system.dto.kujiale.pano.RoamItem;
import com.ruoyi.system.service.KuJiaLeService;
import com.ruoyi.system.utils.PageConvertUtil;
import com.ruoyi.system.utils.SysUserUtils;
import io.swagger.annotations.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 酷家乐设计相关接口
 */
@RestController
@RequestMapping("/system/online/design")
@Api(tags = "酷家乐在线设计相关接口", description = "包含方案基础信息查询、渲染图集分页查询、发布图纸详情查询等功能")
public class KujialeDesignController {

    @Autowired
    private KuJiaLeService kuJiaLeService;


    /**
     * 获取方案基础信息（户型图完整信息）
     * 核心：返回的 data 类型是 HouseTypeResponse（包含层级、基本信息、分层详情）
     */
    @GetMapping("/designBasic/{designBaseId}")
    @ApiOperation(
            value = "获取在线方案基础信息",
            notes = "根据酷家乐设计ID（designBaseId）查询方案的户型图完整信息，包含所有层标识、公共基础信息、分层详情"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "designBaseId",
                    value = "酷家乐设计ID（唯一标识一个设计方案）",
                    required = true,
                    dataType = "String",
                    paramType = "path", // 路径参数标识
                    example = "3FO3461K81DC"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200, // 对应 JYXResult 的 success 状态码
                    message = "查询成功，返回户型图完整信息",
                    response = HouseTypeResponse.class, // data 字段的实际类型
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.DESIGN_BASIC
                            )
                    )
            )
    })
    public JYXResult getDesignBasic(
            @PathVariable String designBaseId) {
        // 明确返回数据类型为 HouseTypeResponse，Swagger 可通过 @ApiResponse 关联
        HouseTypeResponse houseTypeResponse = kuJiaLeService.getDesignBasic(designBaseId);
        return JYXResult.success("查询方案基础信息成功", houseTypeResponse);
    }

    /**
     * 获取渲染图集（分页）
     * 核心：返回的 data 类型是 PageResponse（包含分页信息 + RenderingImage 列表）
     */
    @GetMapping("/renderpic/list")
    @ApiOperation(
            value = "分页获取渲染图集",
            notes = "根据酷家乐设计ID查询该方案下的渲染图列表，支持分页（num最大50），返回分页信息和渲染图详情"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "designBaseId", value = "酷家乐设计ID（关联渲染图所属方案）", required = true, dataType = "String", paramType = "query", example = "3FO3461K81DC"),
            @ApiImplicitParam(name = "currentPage", value = "当前页码（默认1，从1开始）", dataType = "Integer", paramType = "query", defaultValue = "1", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数（默认10，最大50，超过自动取50）", dataType = "Integer", paramType = "query", defaultValue = "10", example = "10")
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "分页查询成功，返回渲染图列表和分页信息",
                    response = PageResponse.class, // data 字段的实际类型（分页对象）
                    examples = @Example(value = @ExampleProperty(mediaType = "application/json", value = SwaggerExampleConstants.KuJiaLeDesign.RENDERPIC_LIST))
            )
    })
    public JYXResult renderpicListPage(
            @RequestParam String designBaseId,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageConvertUtil.PageParam convert = PageConvertUtil.convert(currentPage, pageSize);
        // 明确返回数据类型为 PageResponse，Swagger 可通过 @ApiResponse 关联
        PageResponse<RenderingImage> pageResponse = kuJiaLeService.renderpicList(designBaseId, convert.getStart(), convert.getNum());
        return JYXResult.success("分页查询渲染图集成功", pageResponse);
    }


    /**
     * 获取渲染图集
     * 核心：返回的 data 类型是 PageResponse（包含分页信息 + RenderingImage 列表）
     */
    @GetMapping("/renderpic/list/v2")
    @ApiOperation(
            value = "分页获取渲染图集",
            notes = "根据酷家乐设计ID查询该方案下的渲染图列表，支持分页（num最大50），返回分页信息和渲染图详情"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "designBaseId", value = "酷家乐设计ID（关联渲染图所属方案）", required = true, dataType = "String", paramType = "query", example = "3FO3461K81DC"),
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "返回渲染图集并分组V2",
                    response = RenderingImage.class, // data 字段的实际类型（分页对象）
                    examples = @Example(value = @ExampleProperty(mediaType = "application/json", value = SwaggerExampleConstants.KuJiaLeDesign.RENDERPIC_LIST))
            )
    })
    public JYXResult renderpicListPage(@RequestParam String designBaseId) {
        PageConvertUtil.PageParam convert = PageConvertUtil.convert(1, 50);
        // 明确返回数据类型为 PageResponse，Swagger 可通过 @ApiResponse 关联
        PageResponse<RenderingImage> pageResponse = kuJiaLeService.renderpicList(designBaseId, convert.getStart(), convert.getNum());
        List<RenderingImage> result = pageResponse.getResult();
        if (CollectionUtil.isEmpty(result)) {
            return JYXResult.error("分页查询渲染图集失败");
        }

        Map<Integer, String> picTypeNameMap = ImmutableMap.of(0, "普通图", 1, "全景图", 3, "俯视图");
        List<Map<String, List<RenderingImage>>> resultList = Lists.newArrayList();
        // 添加所有图片
        resultList.add(ImmutableMap.of("所有图片", result));

        // 分组转换
        Map<Integer, List<RenderingImage>> picTypeMap = result.stream().filter(x -> picTypeNameMap.containsKey(x.getPicType())).collect(Collectors.groupingBy(RenderingImage::getPicType));
        Map<String, List<RenderingImage>> roomNameMap = result.stream().filter(x -> StringUtils.isNotBlank(x.getRoomName())).collect(Collectors.groupingBy(RenderingImage::getRoomName));

        if (MapUtil.isNotEmpty(picTypeMap)) {
            picTypeMap.forEach((k, v) -> resultList.add(ImmutableMap.of(picTypeNameMap.getOrDefault(k, "其他图"), v)));
        }

        if (MapUtil.isNotEmpty(roomNameMap)) {
            roomNameMap.forEach((k, v) -> resultList.add(ImmutableMap.of(k, v)));
        }

        return JYXResult.success("查询渲染图集V2成功", roomNameMap);
    }

    /**
     * 获取渲染图集（全量分组）
     * 核心：返回的 data 类型是 List<RenderingResponse>（按分组key归类渲染图列表）
     */
    @GetMapping("/renderpic/all/list") // 确保请求路径正确（与前端调用一致）
    @ApiOperation(
            value = "获取渲染图集（全量分组）",
            notes = "1. 功能：根据酷家乐方案ID，查询该方案下所有渲染图，按分组key（如房间类型）归类；\n" +
                    "2. 必选参数：designBaseId（与「方案基础信息」接口的designBaseId一致，唯一标识方案）；\n" +
                    "3. 响应结构：data为List<RenderingResponse>，每个元素含「分组key」和「该组渲染图列表」；\n" +
                    "4. 关键字段说明：\n" +
                    "   - key：分组标识（如\"livingRoom\"=客厅、\"bedroom\"=主卧）；\n" +
                    "   - picType：渲染图类型（0=普通图，1=全景图，3=俯视图）；\n" +
                    "   - formatCread：创建时间（字符串格式，如\"1小时前\"\"2天前\"）；\n" +
                    "5. 全景图特殊说明：picType=1时，panoLink字段返回全景图访问链接，普通图/俯视图该字段为空。"
    )
    // 接口参数说明（仅designBaseId一个必选参数）
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "designBaseId",
                    value = "酷家乐方案ID（唯一标识一个设计方案，必选）",
                    required = true,          // 标记为必选参数
                    dataType = "String",      // 参数类型（与实体类字段一致）
                    paramType = "query",      // 参数位置（URL查询参数）
                    example = "3FO3461K81DC"  // 示例值（复用项目现有规范）
            )
    })
    // 响应场景说明（成功+失败）
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功：返回全量分组渲染图列表",
                    response = JYXResult.class,  // 外层通用响应类
                    // 引用公共示例类中的成功示例（避免硬编码JSON）
                    examples = @Example(value = @ExampleProperty(mediaType = "application/json", value = SwaggerExampleConstants.KuJiaLeDesign.RENDERPIC_ALL_LIST_SUCCESS))
            ),
            @ApiResponse(
                    code = 500,
                    message = "查询失败：常见场景为「designBaseId不存在」「方案无渲染图」「接口调用异常」",
                    response = JYXResult.class,
                    // 引用公共示例类中的失败示例
                    examples = @Example(value = @ExampleProperty(mediaType = "application/json", value = SwaggerExampleConstants.KuJiaLeDesign.RENDERPIC_ALL_LIST_ERROR)
                    )
            )
    })
    public JYXResult renderpicList(
            @RequestParam String designBaseId) {
        List<RenderingResponse> renderingResponses = kuJiaLeService.renderpicList(designBaseId);
        return JYXResult.success("查询渲染图集成功", renderingResponses);
    }


    /**
     * 获取酷家乐发布图纸详情（最近一次发布）
     * 需先在施工图3.0的图纸管理器中发布图纸
     */
    @GetMapping("/drawing/detail")
    @ApiOperation(
            value = "获取发布图纸详情",
            notes = "根据酷家乐方案ID查询最近一次发布的图纸详情，包含图纸URL、文件夹结构、分类信息；需先在施工图3.0管理器中发布图纸"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "designId",
                    value = "酷家乐方案ID（必选，从方案相关接口获取）",
                    required = true,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO3461K81DC"
            ),
            @ApiImplicitParam(
                    name = "level",
                    value = "楼层（可选，1=地上一层，-1=地下一层，不传默认地上一层）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    example = "1"
            ),
            @ApiImplicitParam(
                    name = "orderId",
                    value = "审核方案ID（可选，无审核时可不传）",
                    required = false,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO4K7ANB7XX"
            ),
            @ApiImplicitParam(
                    name = "publishSource",
                    value = "发布来源（可选，0=施工图环境发布，1=新版户型图环境发布，默认0）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "0",
                    example = "0"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功，返回图纸详情（含URL、文件夹、分类）",
                    // 明确data字段类型
                    response = DrawingDetailData.class
                    ,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.DRAWING_DETAIL
                            )
                    )
            )
    })
    public JYXResult getDrawingDetail(
            @RequestParam String designId,
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) String orderId,
            @RequestParam(required = false, defaultValue = "0") Integer publishSource) {
        // 调用服务层获取图纸详情
        DrawingDetailData drawingDetail = kuJiaLeService.getDrawingDetail(designId, level, orderId, publishSource);
        return JYXResult.success("查询发布图纸详情成功", drawingDetail);
    }


    /**
     * 获取全屋漫游列表（3D漫游+智能漫游）
     */
    @GetMapping("/roam/list")
    @ApiOperation(
            value = "获取全屋漫游列表",
            notes = "1. 功能：根据用户ID和方案ID，查询该方案下的全屋漫游列表（含3D漫游、智能漫游）；\n" +
                    "2. 必选参数：appuid（用户ID）、designId（方案ID），忽略----；\n" +
                    "3. 漫游类型：panoType=1（3D漫游）、panoType=3（智能漫游）；\n" +
                    "4. 用途：roamUrl为直接访问地址，可前端跳转；coverUrl为封面图展示"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "designId",
                    value = "方案ID（必选，与「获取方案基础信息」接口的designBaseId一致）",
                    required = true,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO3461K81DC"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功：返回漫游列表（含类型、封面图、访问地址）",
                    response = JYXResult.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.ROAM_LIST_SUCCESS
                            )
                    )
            ),
            @ApiResponse(
                    code = 500,
                    message = "查询失败：常见场景为「appuid/designId为空」「方案无漫游数据」「接口调用异常」",
                    response = JYXResult.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.ROAM_LIST_ERROR
                            )
                    )
            )
    })
    public JYXResult getRoamList(
//            @RequestParam String appuid,
            @RequestParam String designId) {

        String kujialeId = SysUserUtils.getKujialeId();
        if (StringUtils.isBlank(kujialeId)) {
            return JYXResult.error("没找到你");
        }
        // 调用服务层获取漫游列表
        List<RoamItem> roamList = kuJiaLeService.getRoamList(kujialeId, designId);
        return JYXResult.success("查询全屋漫游列表成功", roamList);
    }


    /**
     * 获取当前用户的方案列表（分页）
     * 支持筛选阶段、排序、模糊查询、增量查询
     */
    @GetMapping("/user/design/list")
    @ApiOperation(
            value = "获取当前用户方案列表",
            notes = "1. 功能：查询当前登录用户的所有方案（户型阶段+装修阶段），支持分页和筛选；\n" +
                    "2. 分页规则：currentPage从1开始，pageSize最大50（超过自动取50）；\n" +
                    "3. 筛选条件：status=0（户型）、1（装修），不传查全部；sort=0（创建时间倒排）、1（修改时间倒排）；\n" +
                    "4. 增量查询：time传时间戳（ms），查询该时间后修改的方案；keyword模糊匹配方案名称。"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "currentPage",
                    value = "当前页码（默认1，从1开始）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "1",
                    example = "1"
            ),
            @ApiImplicitParam(
                    name = "pageSize",
                    value = "每页条数（默认10，最大50）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "10",
                    example = "10"
            ),
            @ApiImplicitParam(
                    name = "status",
                    value = "方案阶段（可选：0=户型阶段，1=装修阶段，不传查全部）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    example = "0"
            ),
            @ApiImplicitParam(
                    name = "sort",
                    value = "排序方式（可选：0=创建时间倒排，1=修改时间倒排，不传默认0）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    example = "0"
            ),
            @ApiImplicitParam(
                    name = "keyword",
                    value = "方案名称模糊查询（可选，不传不查询）",
                    required = false,
                    dataType = "String",
                    paramType = "query",
                    example = "金色"
            ),
            @ApiImplicitParam(
                    name = "time",
                    value = "增量时间戳（可选，ms，查询该时间后修改的方案，不传不限制）",
                    required = false,
                    dataType = "Long",
                    paramType = "query",
                    example = "1560528486000"
            ),
            @ApiImplicitParam(
                    name = "appuid",
                    value = "酷家乐用户id",
                    dataType = "String",
                    paramType = "query",
                    example = "123456"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功：返回分页方案列表（含户型+装修阶段）",
                    response = UserDesignItem.class
            ),
            @ApiResponse(
                    code = 500,
                    message = "查询失败：常见场景为「用户未绑定酷家乐ID」「接口调用异常」",
                    response = JYXResult.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.USER_DESIGN_LIST_ERROR
                            )
                    )
            )
    })
    public JYXResult getUserDesignList(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer sort,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String appuid,
            @RequestParam(required = false) Long time) {
        // 1. 从当前登录用户获取酷家乐ID（appuid），与getRoamList接口逻辑一致
        if (StringUtils.isBlank(appuid)) {
            // 1. 从当前登录用户获取酷家乐ID（appuid），与getRoamList接口逻辑一致
            appuid = SysUserUtils.getKujialeId();
            if (StringUtils.isBlank(appuid)) {
                return JYXResult.error("没有酷家乐ID，无法查询方案列表");
            }
        }

        // 2. 转换分页参数：currentPage→start，pageSize→num（自动限制num≤50）
        PageConvertUtil.PageParam pageParam = PageConvertUtil.convert(currentPage, pageSize);
        int start = pageParam.getStart();
        int num = pageParam.getNum();

        // 3. 调用服务层获取方案列表
        PageResponse<UserDesignItem> designPage = kuJiaLeService.getUserDesignList(appuid, start, num, status, sort, keyword, time);
        return JYXResult.success("查询当前用户方案列表成功", designPage);
    }


    /**
     * 分页查询方案图册信息（调用三方接口）
     * 核心：根据方案ID和图册类型，返回分页图册列表（含渲染中/完成状态）
     */
    @PostMapping("/album/data")
    @ApiOperation(
            value = "分页查询方案图册信息",
            notes = "1. 功能：根据方案ID和图册类型，分页查询方案中的图册数据（支持普通图、生长动画、漫游视频等）；\n" +
                    "2. 必选参数：designId（方案ID）、sceneTypeId（图册类型）、num（每页数量1-50）；\n" +
                    "3. 分页逻辑：首次请求不传lastItemId，后续请求传上页返回的lastItemId；\n" +
                    "4. 图册类型枚举：0=普通图，1=生长动画，2=漫游视频，3=模板视频，4=一键成片，5=伪色图，6=Koolight情景模式；\n" +
                    "5. 状态说明：status=0（渲染中），status=1（完成）。"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "designId",
                    value = "方案ID（与「方案基础信息」接口的designBaseId一致，必选）",
                    required = true,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO3461K81DC"
            ),
            @ApiImplicitParam(
                    name = "sceneTypeId",
                    value = "图册类型（必选，枚举值：0=普通图，1=生长动画，2=漫游视频，3=模板视频，4=一键成片，5=伪色图，6=Koolight情景模式）",
                    required = true,
                    dataType = "Integer",
                    paramType = "query",
                    example = "2"
            ),
            @ApiImplicitParam(
                    name = "lastItemId",
                    value = "上页最后一个图册ID（分页用，首次请求不传，后续请求传上页返回的lastItemId，可选）",
                    required = false,
                    dataType = "String",
                    paramType = "query",
                    example = "3FOL5WHS5MD1"
            ),
            @ApiImplicitParam(
                    name = "num",
                    value = "每页数量（必选，1≤num≤50，超过自动取50）",
                    required = true,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "10",
                    example = "10"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功：返回图册信息（含错误码、图册列表、分页标识）",
                    response = AlbumDataResponse.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.ALBUM_DATA_SUCCESS
                            )
                    )
            ),
            @ApiResponse(
                    code = 500,
                    message = "查询失败：常见场景为「用户未绑定酷家乐ID」「方案ID不存在」「图册类型非法」「三方接口异常」",
                    response = JYXResult.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.ALBUM_DATA_ERROR
                            )
                    )
            )
    })
    public JYXResult getAlbumData(
            @RequestParam String designId,
            @RequestParam Integer sceneTypeId,
            @RequestParam(required = false) String lastItemId,
            @RequestParam(defaultValue = "10") Integer num) {
        // 1. 从当前登录用户获取酷家乐ID（appuid），与现有接口逻辑一致
        String appuid = SysUserUtils.getKujialeId();
        if (StringUtils.isBlank(appuid)) {
            return JYXResult.error("查询图册信息失败：当前用户未绑定酷家乐ID");
        }

        // 2. 调用服务层查询图册信息
        AlbumDataResponse albumData = kuJiaLeService.getAlbumData(appuid, designId, sceneTypeId, lastItemId, num);

        // 3. 返回封装后的成功响应
        return JYXResult.success("分页查询方案图册信息成功", albumData);
    }

    /**
     * 获取存档列表（调用三方接口）
     * 核心：需先在施工图3.0图纸管理器中存档图纸，且用户需拥有存档权限
     */
    @GetMapping("/archive/list")
    @ApiOperation(
            value = "获取方案存档列表",
            notes = "1. 功能：查询方案下的存档图纸列表（支持分页），需先在施工图3.0管理器中完成存档；\n" +
                    "2. 必选参数：designId（方案ID），用户需拥有该方案的存档权限；\n" +
                    "3. 可选参数：\n" +
                    "   - level：楼层（1=地上一层，-1=地下一层，默认1）；\n" +
                    "   - orderId：审核方案ID（无审核时可不传）；\n" +
                    "   - start：起始游标（默认0，分页用）；\n" +
                    "   - num：每页数目（1-10，默认10，num=1返回最新存档）；\n" +
                    "4. 资源类型说明：resource.type=1(DXF)、2(JPG)、3(PDF)、4(ZIP)、5(JSON)、6(DWG)。"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "designId",
                    value = "酷家乐方案ID（与「方案基础信息」接口的designBaseId一致，必选）",
                    required = true,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO3461K81DC"
            ),
            @ApiImplicitParam(
                    name = "level",
                    value = "楼层（可选，1=地上一层，-1=地下一层，默认1）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "1",
                    example = "1"
            ),
            @ApiImplicitParam(
                    name = "orderId",
                    value = "审核方案ID（可选，无审核时可不传）",
                    required = false,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO4K7ANB7XX"
            ),
            @ApiImplicitParam(
                    name = "start",
                    value = "起始游标（可选，分页用，默认0）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "0",
                    example = "0"
            ),
            @ApiImplicitParam(
                    name = "num",
                    value = "每页数目（可选，1≤num≤10，默认10，三方接口限制最大10条）",
                    required = false,
                    dataType = "Integer",
                    paramType = "query",
                    defaultValue = "10",
                    example = "10"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功：返回存档列表（含存档基本信息和资源文件）",
                    response = ArchiveResultItem.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.ARCHIVE_LIST_SUCCESS
                            )
                    )
            ),
            @ApiResponse(
                    code = 500,
                    message = "查询失败：常见场景为「用户未绑定酷家乐ID」「方案无存档」「权限不足」「num超过10」",
                    response = JYXResult.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.ARCHIVE_LIST_ERROR
                            )
                    )
            )
    })
    public JYXResult getArchiveList(
            @RequestParam String designId,
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) String orderId,
            @RequestParam(required = false) Integer start,
            @RequestParam(required = false) Integer num) {
        // 1. 从当前登录用户获取酷家乐ID（appuid），与现有接口逻辑一致
        String appuid = SysUserUtils.getKujialeId();
        if (StringUtils.isBlank(appuid)) {
            return JYXResult.error("获取存档列表失败：当前用户未绑定酷家乐ID");
        }

        // 2. 调用服务层获取存档列表
        ArchiveResultItem archiveList = kuJiaLeService.getArchiveList(appuid, designId, level, orderId, start, num);

        // 3. 返回封装后的成功响应
        return JYXResult.success("查询方案存档列表成功", archiveList);
    }


    /**
     * 获取方案平面布置图片列表（调用三方接口）
     * 核心：返回方案所有楼层的平面布置图，为空表示该楼层未生成图片（需用户手动放置图例）
     */
    @GetMapping("/layout/legend/image")
    @ApiOperation(
            value = "获取方案平面布置图片列表",
            notes = "1. 功能：查询方案所有楼层的平面布置图信息，支持地上/地下楼层；\n" +
                    "2. 必选参数：planId（酷家乐方案planId，可从「方案基础信息」接口的basicInfo.planId获取）；\n" +
                    "3. 响应说明：\n" +
                    "   - levelIndex：楼层序号（1=地上一层，-1=地下一层，与方案层级一致）；\n" +
                    "   - layoutLegendImage：图片URL（为空时需用户进入「平面布置」页面放置图例并保存生成）；\n" +
                    "4. 权限要求：需拥有该方案的查看权限。"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "planId",
                    value = "酷家乐方案planId（必选，唯一标识方案户型，示例：3FO3IBYSUC5T）",
                    required = true,
                    dataType = "String",
                    paramType = "query",
                    example = "3FO3IBYSUC5T"
            )
    })
    @ApiResponses({
            @ApiResponse(
                    code = 200,
                    message = "查询成功：返回所有楼层的平面布置图信息（含空值楼层）",
                    response = LayoutLegendItem.class,
                    examples = @Example(
                            value = @ExampleProperty(
                                    mediaType = "application/json",
                                    value = SwaggerExampleConstants.KuJiaLeDesign.LAYOUT_LEGEND_SUCCESS
                            )
                    )
            )
    })
    public JYXResult getLayoutLegendImage(
            @RequestParam String planId) {
        return JYXResult.success("查询平面布置图片列表成功", kuJiaLeService.getLayoutLegendImage(planId));
    }

}